/*
 * @author Robert Hanwell
 * @version 3
 * @since January
 */
package com.smartbox.floodaction;

import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ExpandableListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.TextView;
import android.widget.Toast;

// TODO: Auto-generated Javadoc
/**
 * The Class TabGrpActivity.
 */
public class TabGrpActivity extends ExpandableListActivity {

	/** The db helper. */
	private GroupActionData dbHelper; // your db adapter

	/** The m groups cursor. */
	private Cursor mGroupsCursor; // cursor for list of groups (list top nodes)

	/** The m group id column index. */
	private int mGroupIdColumnIndex;

	/** The m adapter. */
	private ExpandableListAdapter mAdapter;

	/** The row id. */
	private long rowID = 0;

	/** The grp activity. */
	private Long grpActivity;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		dbHelper = new GroupActionData(this);
		dbHelper.open();
		fillData();
		ListView list = getExpandableListView();
		list.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {

				grpActivity = mGroupsCursor.getLong(mGroupsCursor
						.getColumnIndex(SQLHelper.KEY_ACT_ROWID));
				getAlertOptions();
				// Return true to consume the click event. In this case the
				// onListItemClick listener is not called anymore.
				return true;
			}
		});
	}

	/**
	 * Fill data.
	 */
	private void fillData() {
		mGroupsCursor = dbHelper.getGroupByAction(); // fills cursor with list
														// of
														// your top nodes -
														// groups
		startManagingCursor(mGroupsCursor);

		// Cache the ID column index
		mGroupIdColumnIndex = mGroupsCursor
				.getColumnIndexOrThrow(SQLHelper.KEY_ACT_ROWID);

		// Set up our adapter
		mAdapter = new MyExpandableListAdapter(mGroupsCursor, this,

		android.R.layout.simple_expandable_list_item_1,
				R.layout.list_example_entry,

				new String[] { SQLHelper.KEY_ACT_PLAN_NAME }, // group title for
																// group layouts
				new int[] { android.R.id.text1 },

				new String[] { SQLHelper.KEY_TSK_NAME }, // exercise title for
															// child layouts
				new int[] { R.id.name_entry });

		setListAdapter(mAdapter);
	}

	/**
	 * Gets the alert options.
	 * 
	 * @return the alert options
	 */
	public void getAlertOptions() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Group Activity Submited.");
		builder.setCancelable(true);
		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.cancel();
					}

				});
		builder.setPositiveButton("Edit Group Action",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {

						Intent i = new Intent(
								"com.smartbox.floodaction.CREATEEDITGROUPACTION");
						i.putExtra("grpActId", grpActivity);
						startActivity(i);
						Toast.makeText(TabGrpActivity.this,
								"Edit The Activity", Toast.LENGTH_LONG).show();
					}

				});
		builder.setNeutralButton("Delete",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int id) {
						AlertWarning();

					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Alert warning.
	 */
	public void AlertWarning() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Are you sure?");
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setCancelable(true);
		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.cancel();
					}

				});
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int id) {
				int row = 0;
				row = dbHelper.deleteGroupActionCategory(grpActivity);
				if (row > 0) {
					Toast.makeText(TabGrpActivity.this, "Succsess Deleted",
							Toast.LENGTH_LONG).show();
					fillData();

				}
				dialog.cancel();
			}

		});
		AlertDialog alert = builder.create();
		alert.show();

	}

	// extending SimpleCursorTreeAdapter
	/**
	 * The Class MyExpandableListAdapter.
	 */
	public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * android.widget.SimpleCursorTreeAdapter#bindGroupView(android.view
		 * .View, android.content.Context, android.database.Cursor, boolean)
		 */
		@Override
		protected void bindGroupView(View view, Context context, Cursor cursor,
				boolean isExpanded) {
			// TODO Auto-generated method stub
			super.bindGroupView(view, context, cursor, isExpanded);
		}

		/**
		 * Instantiates a new my expandable list adapter.
		 * 
		 * @param cursor
		 *            the cursor
		 * @param context
		 *            the context
		 * @param groupLayout
		 *            the group layout
		 * @param childLayout
		 *            the child layout
		 * @param groupFrom
		 *            the group from
		 * @param groupTo
		 *            the group to
		 * @param childrenFrom
		 *            the children from
		 * @param childrenTo
		 *            the children to
		 */
		public MyExpandableListAdapter(Cursor cursor, Context context,
				int groupLayout, int childLayout, String[] groupFrom,
				int[] groupTo, String[] childrenFrom, int[] childrenTo) {
			super(context, cursor, groupLayout, groupFrom, groupTo,
					childLayout, childrenFrom, childrenTo);
		}

		// returns cursor with subitems for given group cursor
		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * android.widget.CursorTreeAdapter#getChildrenCursor(android.database
		 * .Cursor)
		 */
		@Override
		protected Cursor getChildrenCursor(Cursor groupCursor) {
			Cursor taskCursor = dbHelper.getTaskByAction(groupCursor
					.getLong(mGroupIdColumnIndex));
			startManagingCursor(taskCursor);
			return taskCursor;
		}

		// I needed to process click on click of the button on child item
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.CursorTreeAdapter#getChildView(int, int, boolean,
		 * android.view.View, android.view.ViewGroup)
		 */
		@Override
		public View getChildView(final int groupPosition,
				final int childPosition, boolean isLastChild, View convertView,
				ViewGroup parent) {
			View rowView = super.getChildView(groupPosition, childPosition,
					isLastChild, convertView, parent);

			TextView details = (TextView) rowView.findViewById(R.id.name_entry);

			details.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					Cursor taskCursor = getChild(groupPosition, childPosition);

					Long taskId = taskCursor.getLong(taskCursor
							.getColumnIndex(SQLHelper.KEY_ACT_ROWID));

					Intent i = new Intent(
							"com.smartbox.floodaction.SINGLETASKVIEW");
					i.putExtra("taskId", taskId);
					startActivity(i);
				}
			});

			return rowView;

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater menuInf = getMenuInflater();
		menuInf.inflate(R.menu.create_menu, menu);

		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.bCreate:
			rowID = 0;
			Intent i = new Intent(
					"com.smartbox.floodaction.CREATEEDITGROUPACTION");
			i.putExtra("ComPK", rowID);
			startActivity(i);
			finish();
			return true;
		case R.id.bEdit:

			return true;

		}
		return false;
	}

	// after create start database connection
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		Log.i("onStart", "open database");
		dbHelper.open();

	}

	// before we close the screen close the database
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		Log.i("onStop", "closing database");
		dbHelper.close();

	}

}
